package com.mofang.ticketing.user.serve.account.controller;


import com.mofang.ticketing.common.core.result.Result;
import com.mofang.ticketing.common.core.urls.user.account.AuthUrl;
import com.mofang.ticketing.common.log.SystemLog;
import com.mofang.ticketing.user.mapping.entity.SysUserDO;
import com.mofang.ticketing.user.serve.account.dto.request.AccountLoginReqDTO;
import com.mofang.ticketing.user.serve.account.dto.request.AppendPermissionReqDTO;
import com.mofang.ticketing.user.serve.account.dto.request.InitPlatformAdminAccountReqDTO;
import com.mofang.ticketing.user.serve.account.dto.response.AccountLoinRespDTO;
import com.mofang.ticketing.user.client.pojo.response.AuthUserBelongMerchantRespDTO;
import com.mofang.ticketing.user.serve.account.dto.response.CurrentAccountInfoRespDTO;
import com.mofang.ticketing.user.serve.account.service.AuthService;
import com.mofang.ticketing.user.serve.account.bo.Permission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @author LGC
 */
@Slf4j
@Api(tags = "后台认证")
@RestController
public class AuthController {
    @Resource
    private AuthService authService;


    @ApiOperation(value = "初始化权限")
    @PostMapping(value = AuthUrl.INIT_PERMISSION)
    public Result<Void> initPermission() {
        authService.initializePermission();
        return Result.success();
    }

    @SystemLog
    @ApiOperation(value = "追加权限")
    @PostMapping(value = AuthUrl.APPEND_PERMISSION)
    public Result<List<Permission>> appendPermission(@Valid @RequestBody AppendPermissionReqDTO dto) {
        return Result.success(authService.appendPermission(dto));
    }

    @SystemLog
    @ApiOperation(value = "初始化平台管理员")
    @PostMapping(value = AuthUrl.INIT_PLATFORM_ADMIN)
    public Result<SysUserDO> initPlatformAdmin(@Valid @RequestBody InitPlatformAdminAccountReqDTO dto) {
        return Result.success(authService.initPlatformAdminAccount(dto));
    }

    @ApiOperation(value = "登录")
    @PostMapping(value = AuthUrl.LOGIN)
    public Result<AccountLoinRespDTO> login(@Valid @RequestBody AccountLoginReqDTO dto) {
        return Result.success(authService.login(dto));
    }

    @ApiOperation(value = "退出登录")
    @PostMapping(value = AuthUrl.LOGOUT)
    public Result<AccountLoinRespDTO> logout() {
        authService.logout();
        return Result.success();
    }

    //    @PreAuthorize("@ss.hasPerm('b')")
    @ApiOperation(value = "当前登录用户信息")
    @PostMapping(value = AuthUrl.CURRENT_ACCOUNT_INFO)
    public Result<CurrentAccountInfoRespDTO> currentAccountInfo() {
        return Result.success(authService.currentAccountInfo());
    }

    /*********************************** rpc ****************************************/

    @ApiOperation(value = "获取认证用户所属商户")
    @PostMapping(value = AuthUrl.AUTH_BELONG_MERCHANTS)
    public Result<List<AuthUserBelongMerchantRespDTO>> authBelongMerchants() {
        return Result.success(authService.authBelongMerchants());
    }

    @ApiOperation(value = "获取认证用户所属商户ID")
    @PostMapping(value = AuthUrl.AUTH_BELONG_MERCHANT_IDS)
    public Result<List<Long>> authBelongMerchantIds() {
        return Result.success(authService.authBelongMerchantIds());
    }

}
